package com.hcbd.tdwt.base;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.hcbd.tdwt.base.bd.BdToolFactory;
import com.hcbd.tdwt.base.bd.XYSerialPortManage;
import com.hcbd.tdwt.base.hcbd.BdXyACKInfo;
import com.hcbd.tdwt.base.hcbd.BdXyPwiInfo;
import com.hcbd.tdwt.base.hcbd.BdXyZFIInfo;
import com.hcbd.tdwt.base.hcbd.CCPRSHcbdInfo;
import com.hcbd.tdwt.base.other.BeeAndVibrateManager;
import com.hcbd.tdwt.base.hcbd.BDddxInfo;
import com.hcbd.tdwt.ui.xingyu.vo.PositionVo;
import com.hcbd.tdwt.util.AppUtil;
import com.hcbd.tdwt.MainApp;
import com.hcbd.tdwt.util.DateUtils;
import com.hcbd.tdwt.util.FileUtil;
import com.hcbd.tdwt.util.FileUtils;
import com.hcbd.tdwt.util.SharedPreferencesUtil;
import com.hcbd.tdwt.util.StringUtil;
import com.pangu.bdsdk2021.bluetooth.BluetoothManage;
import com.pangu.bdsdk2021.entity.extensionterminal.BDBTXInfo;
import com.pangu.bdsdk2021.entity.extensionterminal.BDCEXInfo;
import com.pangu.bdsdk2021.entity.extensionterminal.BDMDXInfo;
import com.pangu.bdsdk2021.entity.extensionterminal.BDRNXInfo;
import com.pangu.bdsdk2021.entity.extensionterminal.BDUGXInfo;
import com.pangu.bdsdk2021.entity.extensionterminal.BDYPXInfo;
import com.pangu.bdsdk2021.entity.extensionterminal.CCPRSInfo;
import com.pangu.bdsdk2021.entity.extensionterminal.DBJXXInfo;
import com.pangu.bdsdk2021.entity.extensionterminal.DbBxxInfo;
import com.pangu.bdsdk2021.entity.extensionterminal.DdLxxInfo;
import com.pangu.bdsdk2021.entity.extensionterminal.DlYxxInfo;
import com.pangu.bdsdk2021.entity.extensionterminal.DmSxxInfo;
import com.pangu.bdsdk2021.entity.extensionterminal.DpWxxInfo;
import com.pangu.bdsdk2021.entity.extensionterminal.DwZxxInfo;
import com.pangu.bdsdk2021.entity.terminalthreeofficial.BdFkiInfo;
import com.pangu.bdsdk2021.entity.terminalthreeofficial.BdIcpInfo;
import com.pangu.bdsdk2021.entity.terminalthreeofficial.BdPwiInfo;
import com.pangu.bdsdk2021.entity.terminalthreeofficial.BdTciInfo;
import com.pangu.bdsdk2021.terminal.TerminalReceiveManager;
import com.pangu.bdsdk2021.terminal.listener.TerminalListener2_1;
import com.hcbd.tdwt.ui.xingyu.vo.BoxContact;
import com.hcbd.tdwt.ui.xingyu.vo.ResultNumberVo;
import com.hcbd.tdwt.ui.xingyu.vo.UserMessageVO;
import com.pangu.bdsdk2021.bluetooth.BluetoothManageInterface;
import com.pangu.bdsdk2021.entity.terminalone.BDFKInfo;
import com.pangu.bdsdk2021.entity.terminalone.BDGLInfo;
import com.pangu.bdsdk2021.entity.terminalone.BDICInfo;
import com.pangu.bdsdk2021.entity.terminalone.BDMSHInfo;
import com.pangu.bdsdk2021.entity.terminalone.BDOKInfo;
import com.pangu.bdsdk2021.entity.terminalone.BDPWInfo;
import com.pangu.bdsdk2021.entity.terminalone.BDSOSInfo;
import com.pangu.bdsdk2021.entity.terminalone.BDTXInfo;
import com.pangu.bdsdk2021.entity.terminalone.BDTraceInfo;
import com.pangu.bdsdk2021.entity.terminalone.BDVRInfo;
import com.pangu.bdsdk2021.entity.terminalthree.BDIMEIInfo;
import com.pangu.bdsdk2021.entity.terminalthree.BDLxxInfo;
import com.pangu.bdsdk2021.entity.terminalthree.BDMsgInfo;
import com.pangu.bdsdk2021.entity.terminalthree.BDSNXInfo;
import com.pangu.bdsdk2021.entity.terminalthree.BDSXXInfo;
import com.pangu.bdsdk2021.entity.terminalthree.BDSnrInfo;
import com.pangu.bdsdk2021.entity.terminalthree.BDVXXInfo;
import com.pangu.bdsdk2021.entity.terminalthree.BDZXXInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BCKLTInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BCWAAInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDADXInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDALFInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDALMInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDDVXInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDDWRInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDECTInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDGGAInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDGLLInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDGLSInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDGSAInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDGSVInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDHZRInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDICZInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDRMCInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDTXTInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDVTGInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDWAXInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDZDAInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDZDXInfo;
import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class BdManager  implements TerminalListener2_1,BluetoothManageInterface {
    private static BdManager bdManager;
    private static String TAG = "BdManager";
    //三级卡
    public static final int LEVEL_ONE_MAX_TEXT_SIZE=40;
    public static final int LEVEL_TWO_MAX_TEXT_SIZE=115;
    public static final int LEVEL_THREE_MAX_TEXT_SIZE=260;
    public static final int LEVEL_FOUR_MAX_TEXT_SIZE=550;
    public static final int LEVEL_FIVE_MAX_TEXT_SIZE=980;



    public int send_size=0;
    public static BdManager getInstance() {
        if (bdManager == null) {
            bdManager = new BdManager();
        }
        return bdManager;
    }

    private BdManager(){
        logPath= FileUtil.getLogFile();
    }
    public void init(Context context){//初始化并设置要接收的指令对象
        TerminalReceiveManager terminalManageTool=TerminalReceiveManager.getInstance();
        terminalManageTool.initTerminalTwo();
        terminalManageTool.initTerminalThree();
        terminalManageTool.initTerminalThreeOfficial();
        terminalManageTool.setTerminalListener(bdManager);
        terminalManageTool.insertReceiveMsg(new CCPRSHcbdInfo());
        terminalManageTool.insertReceiveMsg(new BdXyPwiInfo());
        terminalManageTool.insertReceiveMsg(new BDddxInfo());
        BluetoothManage.getInstance().init(context,bdManager);
    }
    public void initThreeOfficial(Context context){
        TerminalReceiveManager.getInstance().initTerminalTwo();
        TerminalReceiveManager.getInstance().initExtensionTerminal();
        TerminalReceiveManager.getInstance().initTerminalThreeOfficial();
        DdLxxInfo ddLxxInfo=new DdLxxInfo();
        TerminalReceiveManager.getInstance().insertReceiveMsg(ddLxxInfo);
        TerminalReceiveManager.getInstance().setTerminalListener(bdManager);
        BluetoothManage.getInstance().init(context,bdManager);
    }
    @Override
    public void onZDXInfo(BDZDXInfo bdzdxInfo) {
        if(bdzdxInfo!=null) {
            BeidouBoxParams.kwh = Integer.parseInt(bdzdxInfo.batteryPower);
            EventBus.getDefault().post(bdzdxInfo);
        }
    }

    @Override
    public void onALMInfo(BDALMInfo bdalmInfo) {
        if(bdalmInfo!=null){
            EventBus.getDefault().post(bdalmInfo);
        }
    }

    @Override
    public void onZDAInfo(BDZDAInfo bdzdaInfo) {
        if(bdzdaInfo!=null){
            EventBus.getDefault().post(bdzdaInfo);
        }
    }

    @Override
    public void onECTInfo(BDECTInfo bdectInfo) {

    }

    @Override
    public void onBSIInfo(BDGLInfo bdglInfo) {

    }

    @Override
    public void onDWRInfo(BDDWRInfo bddwrInfo) {

    }

    @Override
    public void onICZInfo(BDICZInfo bdiczInfo) {

    }

    @Override
    public void onKLTInfo(BCKLTInfo bckltInfo) {

    }

    @Override
    public void onHZRInfo(BDHZRInfo bdhzrInfo) {

    }

    @Override
    public void onWAAInfo(BCWAAInfo bcwaaInfo) {

    }

    @Override
    public void onGGAInfo(BDGGAInfo bdggaInfo) {
        if(bdggaInfo!=null){
            try{
                if(bdggaInfo.statusIndication.equals("1")){
                    double latitude = StringUtil.analysisLonlat(bdggaInfo.latitude);
                    double longitude = StringUtil.analysisLonlat(bdggaInfo.longitude);
                    // 四舍五入
                    latitude = StringUtil.roundDouble(latitude,6,false);
                    longitude = StringUtil.roundDouble(longitude,6,false);
                    // 设备位置使用北斗指令解析
                    BeidouBoxParams.gpsLatDou = latitude;
                    BeidouBoxParams.gpsLonDou = longitude;
                    BeidouBoxParams.gpsAltitude = Double.parseDouble(bdggaInfo.antennaHeight);
                    SharedPreferencesUtil sp = MainApp.getInstance().sp;
                    int format = sp.getAttributeInt(Constant.LATITUDE_AND_LONGITUDE_FORMAT);
                    if(Constant.DEGREE_MINUTES_SECONDS_FORMAT== format){
                        BeidouBoxParams.gpsLatitude = StringUtil.changeToDFM(BeidouBoxParams.gpsLatDou);
                        BeidouBoxParams.gpsLongitude = StringUtil.changeToDFM(BeidouBoxParams.gpsLonDou);
                    }else if(Constant.DEGREE_FORMAT== format){
                        BeidouBoxParams.gpsLatitude = String.valueOf(BeidouBoxParams.gpsLatDou);
                        BeidouBoxParams.gpsLongitude = String.valueOf(BeidouBoxParams.gpsLonDou);
                    }

//                    MainApp.getInstance().showMsg("GGA:longitude-"+bdggaInfo.longitude+",latitude-"+bdggaInfo.latitude);

                    EventBus.getDefault().post(bdggaInfo);
                    if(MainApp.getInstance().locationInfoInterface!=null){
                        MainApp.getInstance().locationInfoInterface.LocationInfo();
                        Log.e(TAG, "locationInfoInterface执行");
                    }else {
                        Log.e(TAG, "locationInfoInterface为空");
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onGSVInfo(BDGSVInfo bdgsvInfo) {
        if(bdgsvInfo!=null){
            EventBus.getDefault().post(bdgsvInfo);
//            MainApp.getInstance().showMsg("GSV:satellitesNumber-"+bdgsvInfo.satellitesNumber+",satellitesType-"+bdgsvInfo.satellitesType);
        }
    }

    @Override
    public void onGLLInfo(BDGLLInfo bdgllInfo) {

    }

    @Override
    public void onGSAInfo(BDGSAInfo bdgsaInfo) {

    }

    // RMC
    @Override
    public void onRMDInfo(BDRMCInfo bdrmcInfo) {
        if(bdrmcInfo!=null){
            try{
                FileUtils.writeFile(logPath,"bdrmcInfo:longitude-"+bdrmcInfo.longitude+",latitude-"+bdrmcInfo.latitude);
                if(!bdrmcInfo.utcTime.equals("")){BeidouBoxParams.bdTime = DateUtils.turnBDTimeToDate(bdrmcInfo.utcTime);}  // 卫星时间
                if(bdrmcInfo.positionStatus.equals("A")){
                    double latitude = StringUtil.analysisLonlat(bdrmcInfo.latitude);
                    double longitude = StringUtil.analysisLonlat(bdrmcInfo.longitude);
                    // 四舍五入
                    latitude = StringUtil.roundDouble(latitude,6,false);
                    longitude = StringUtil.roundDouble(longitude,6,false);
                    // 设备位置使用北斗指令解析
                    BeidouBoxParams.gpsLatDou = latitude;
                    BeidouBoxParams.gpsLonDou = longitude;
                    SharedPreferencesUtil sp = MainApp.getInstance().sp;
                    int format = sp.getAttributeInt(Constant.LATITUDE_AND_LONGITUDE_FORMAT);
                    if(Constant.DEGREE_MINUTES_SECONDS_FORMAT== format){
                        BeidouBoxParams.gpsLatitude =StringUtil.changeToDFM(BeidouBoxParams.gpsLatDou);
                        BeidouBoxParams.gpsLongitude =StringUtil.changeToDFM(BeidouBoxParams.gpsLonDou);
                    }else if(Constant.DEGREE_FORMAT== format){
                        BeidouBoxParams.gpsLatitude =String.valueOf(BeidouBoxParams.gpsLatDou);
                        BeidouBoxParams.gpsLongitude =String.valueOf(BeidouBoxParams.gpsLonDou);
                    }
                    PositionVo position = new PositionVo();
                    position.setLatitude(latitude);
                    position.setLongitude(longitude);
                    if(TextUtils.isEmpty(bdrmcInfo.groundSpeed)){
                        position.setSpeed(0);
                    }else{
                        position.setSpeed(Double.parseDouble(bdrmcInfo.groundSpeed)*5.144d);
                    }
                    if(TextUtils.isEmpty(bdrmcInfo.groundHeading)){
                        position.setBearing(0);
                    }else{
                        position.setBearing(Double.parseDouble(bdrmcInfo.groundHeading));
                    }
                    position.setAltitude(0);
                    position.setTime(Long.parseLong(DateUtils.getTimeStamp()));
                    if (GlobalParams.positions.size() > 600) {
                        GlobalParams.positions.remove(0);
                    }
                    GlobalParams.positions.add(position);
//                    MainApp.getInstance().showMsg("RMC:longitude-"+bdrmcInfo.longitude+",latitude-"+bdrmcInfo.latitude);
                    // 通知其他页面改变位置
                    EventBus.getDefault().post(bdrmcInfo);
                    if(MainApp.getInstance().locationInfoInterface!=null){
                        MainApp.getInstance().locationInfoInterface.LocationInfo();
                        Log.e(TAG, "locationInfoInterface执行");
                    }else {
                        Log.e(TAG, "locationInfoInterface为空");
                    }
                }
                FileUtils.writeFile(logPath,"onRMD:end");
            }catch (Exception e){
                e.printStackTrace();
                FileUtils.writeFile(logPath,"onRMD-error:"+e.getMessage());
            }
        }
    }

    @Override
    public void onTXTInfo(BDTXTInfo bdtxtInfo) {

    }

    @Override
    public void onGLSInfo(BDGLSInfo bdglsInfo) {

    }

    @Override
    public void onALFInfo(BDALFInfo bdalfInfo) {

    }

    @Override
    public void onVTGInfo(BDVTGInfo bdvtgInfo) {

    }

    @Override
    public void onBDADXInfo(BDADXInfo bdadxInfo) {
        if(bdadxInfo!=null){
            EventBus.getDefault().post(bdadxInfo);
        }
    }

    @Override
    public void onPWInfo(BDPWInfo bdpwInfo) {

    }

    @Override
    public void onBDSnrInfo(BDSnrInfo bdSnrInfo) {
        EventBus.getDefault().post(bdSnrInfo);
    }


    @Override
    public void onFKInfo(BDFKInfo bdfkInfo) {
        FileUtils.writeFile(logPath,"-bdfkInfo");
        if(bdfkInfo!=null){
            if(BeidouBoxParams.lastMsg!=null) {
                boolean no=false;
                if ("Y".equals(bdfkInfo.state)) {
                    BeidouBoxParams.lastMsg.setStatus(Constant.SEND_MSG_SUCCESS);
                    MainApp.getInstance().showMsg("发送成功");

                } else if ("N".equals(bdfkInfo.state)) {
                    no=true;
                    BeidouBoxParams.sentWaitSec = 0;
                    BeidouBoxParams.lastMsg.setStatus(Constant.SEND_MSG_FAIL);
                    MainApp.getInstance().showMsg("发送失败");
                }
                BeidouBoxParams.lastMsg.update();
                ResultNumberVo resultNumberVo=new ResultNumberVo();
                resultNumberVo.setNumber(BeidouBoxParams.lastMsg.getReceiverNumber());
                EventBus.getDefault().post(resultNumberVo);
                if(no){BeidouBoxParams.lastMsg = null;}
            }else{
                if ("Y".equals(bdfkInfo.state)) {
                    MainApp.getInstance().showMsg("发送成功");
                } else if ("N".equals(bdfkInfo.state)) {
                    MainApp.getInstance().showMsg("发送失败");
                }
            }
        }
    }
    public void onBdZFIInfo(BdXyZFIInfo bdXyZFIInfo) {
        if(bdXyZFIInfo != null) {
            Log.e(BdManager.class.getSimpleName(), "onBdZFIInfo: " );
            if ("0".equals(bdXyZFIInfo.type)) {
                if ("0".equals(bdXyZFIInfo.result)) {
                    MainApp.getInstance().showMsg("自毁成功");
//                    XYSerialPortManage.getInstance().onPackageICR();
                    countTimeByInterval();
                } else {
                    MainApp.getInstance().showMsg("自毁失败");
                }
            }
        }
    }

    /**
     * 自毁后多次读取信息
     */
    private Disposable mDisposable;
    public void countTimeByInterval() {
        // 总时间
        int countTime = 2;
        Observable.interval(2000, 2000, TimeUnit.MILLISECONDS)
                // 设置总共发送的次数
                .take(countTime + 1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        // aLong从0开始
                        return countTime - aLong;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable = d;
                        long begin = System.currentTimeMillis();
                        Log.d("baorant  onSubscribe", String.valueOf(begin));
                    }

                    @Override
                    public void onNext(Long value) {
                        Log.d("baorant  onNext",""+value);
                        XYSerialPortManage.getInstance().onPackageICR();
                    }

                    @Override
                    public void onError(Throwable e) {
                        long end = System.currentTimeMillis();
                        Log.d("baorant  onError", String.valueOf(end));
                    }

                    @Override
                    public void onComplete() {
                        long end = System.currentTimeMillis();
                        Log.d("baorant  onComplete", String.valueOf(end));
                        closeTimer();
                    }
                });


    }

    /**
     * 关闭定时器
     */
    public void closeTimer(){
        if (mDisposable != null) {
            mDisposable.dispose();
        }
    }




    @Override
    public void onMXXInfo(BDMsgInfo bdMsgInfo) {

    }

    @Override
    public void onBDSXXInfo(BDSXXInfo bdsxxInfo) {
        if(bdsxxInfo!=null){
            BeidouBoxParams.cardType=Integer.parseInt(bdsxxInfo.cardType);
            if(BeidouBoxParams.cardType==0){
                BeidouBoxParams.cardType=5;
            }
            //BeidouBoxParams.cardType=5;
            initSendSize();
            BeidouBoxParams.userCardNumber=bdsxxInfo.user_id;
            BeidouBoxParams.sendFreq=Integer.parseInt(bdsxxInfo.regional_service_frequency);
            BeidouBoxParams.isBoxConnectNormal = true;
            EventBus.getDefault().post(bdsxxInfo);
        }
    }
    public void initSendSize(){
        if(BeidouBoxParams.cardType==1){
            send_size=LEVEL_ONE_MAX_TEXT_SIZE;
        }else  if(BeidouBoxParams.cardType==2){
            send_size=LEVEL_TWO_MAX_TEXT_SIZE;
        }else if(BeidouBoxParams.cardType==3){
            send_size=LEVEL_THREE_MAX_TEXT_SIZE;
        }else  if(BeidouBoxParams.cardType==4){
            send_size=LEVEL_FOUR_MAX_TEXT_SIZE;
        }else{
            send_size=LEVEL_FIVE_MAX_TEXT_SIZE;
        }
    }
    @Override
    public void onBDVXXInfo(BDVXXInfo bdvxxInfo) {

    }

    @Override
    public void onBDSNXInfo(BDSNXInfo bdsnxInfo) {

    }

    @Override
    public void onBDLXXInfo(BDLxxInfo bdLxxInfo) {

    }

    @Override
    public void onBDIMEIInfo(BDIMEIInfo bdimeiInfo) {

    }

    @Override
    public void onBDZXXInfo(BDZXXInfo bdzxxInfo) {

    }

    @Override
    public void onBdIcpInfo(BdIcpInfo bdIcpInfo) {
        Log.e("bdIcpInfo:",bdIcpInfo.getIDNumber());
        BeidouBoxParams.ASecretIdentity=bdIcpInfo.getASecretIdentity();
        BDSXXInfo bdsxxInfo=new BDSXXInfo();
        bdsxxInfo.user_id=bdIcpInfo.getIDNumber();
        bdsxxInfo.regional_service_frequency=bdIcpInfo.getRDSS_ServiceFrequency();
        bdsxxInfo.cardType=bdIcpInfo.getRDSS_CommunicationLengthLevel();
        String userId=bdIcpInfo.getTheUserId();
        if("2".equals(userId)){
            BeidouBoxParams.card_model=2;
        }
        bdManager.onBDSXXInfo(bdsxxInfo);
    }

    @Override
    public void onBdFkiInfo(BdFkiInfo bdFkiInfo) {
        BDFKInfo bdfkInfo=new BDFKInfo();
        if (bdFkiInfo.getComExeSituation()) {
            bdfkInfo.state=Constant.Y;
        } else {
            bdfkInfo.state=Constant.N;
        }
        bdManager.onFKInfo(bdfkInfo);
    }

    @Override
    public void onBdPwiInfo(BdPwiInfo bdPwiInfo) {
        if(BeidouBoxParams.card_model==2){
            refreshSnrInfo(onReceiveBDPWI(bdPwiInfo.instruc.split(",")));
        }else{
            refreshSnrInfo(bdPwiInfo.getArray());
        }
    }

    @Override
    public void onBdTciInfo(BdTciInfo bdTciInfo) {
        BDTXInfo bdtxInfo=new BDTXInfo();
        bdtxInfo.messageType=Integer.parseInt(bdTciInfo.getCodingCategories());
        bdtxInfo.content=bdTciInfo.getCommunicationData();
        bdtxInfo.fromNumber=Integer.parseInt(bdTciInfo.getTheReceivingPartyID());
        bdManager.onTXInfo(bdtxInfo);
    }

    @Override
    public void onDwZxxInfo(DwZxxInfo dwZxxInfo) {
        EventBus.getDefault().post(dwZxxInfo);
    }

    @Override
    public void onDBJXXInfo(DBJXXInfo dbjxxInfo) {
        EventBus.getDefault().post(dbjxxInfo);
    }

    @Override
    public void onDlYxxInfo(DlYxxInfo dlYxxInfo) {
        EventBus.getDefault().post(dlYxxInfo);
    }

    @Override
    public void onDdLxxInfo(DdLxxInfo ddLxxInfo) {
        try {
            BeidouBoxParams.kwh = Integer.parseInt(ddLxxInfo.power);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onDmSxxInfo(DmSxxInfo dmSxxInfo) {
        EventBus.getDefault().post(dmSxxInfo);
    }

    @Override
    public void onDBBxxInfo(DbBxxInfo dbBxxInfo) {

    }

    @Override
    public void onDpWxxInfo(DpWxxInfo dpWxxInfo) {

    }
    private String logPath;
    @Override
    public void onAllInstructions(String s) {
        FileUtils.writeFile(logPath,s);
    }





    @Override
    public  void onTXInfo(BDTXInfo bdtxInfo) {
        if(bdtxInfo!=null){
            UserMessageVO userMessageVO =ConvertToUserMsgTool.convertMessage(bdtxInfo);
            if(userMessageVO!=null){
                userMessageVO.insert();
                BoxContact.cacheFromBoxId(String.valueOf(bdtxInfo.fromNumber),userMessageVO.getMsgType(),userMessageVO.getContent(),userMessageVO.getLongitude(),userMessageVO.getLatitude(),false);
                updateRecentChatTable(bdtxInfo.content);
                EventBus.getDefault().post(userMessageVO);
            }
        }
    }

    @Override
    public void onVRInfo(BDVRInfo bdvrInfo) {
        if(bdvrInfo!=null){
            EventBus.getDefault().post(bdvrInfo);
        }

    }

    @Override
    public void onICInfo(BDICInfo bdicInfo) {
        if(bdicInfo!=null){
            // BeidouBoxParams.cardType=5;
            if(bdicInfo.communicationLevel==0){
                BeidouBoxParams.cardType=5;
            }else{
                BeidouBoxParams.cardType=bdicInfo.communicationLevel;
            }
            initSendSize();
            BeidouBoxParams.userCardNumber=String.valueOf(bdicInfo.carNumber);
            BeidouBoxParams.sendFreq=bdicInfo.frequency;
            EventBus.getDefault().post(bdicInfo);
        }
    }

    @Override
    public void onWAXInfo(BDWAXInfo bdwaxInfo) {

    }

    @Override
    public void onMSHInfo(BDMSHInfo bdmshInfo) {
        if(bdmshInfo!=null){
            EventBus.getDefault().post(bdmshInfo);
        }
    }

    @Override
    public void onSOSInfo(BDSOSInfo bdsosInfo) {
        if(bdsosInfo!=null){
            EventBus.getDefault().post(bdsosInfo);
        }
    }

    @Override
    public void onTraceSwitchInfo(BDTraceInfo.SwitchInfo switchInfo) {

    }

    @Override
    public void onDVXInfo(BDDVXInfo bddvxInfo) {
        if(bddvxInfo!=null){
            EventBus.getDefault().post(bddvxInfo);
        }
    }

    @Override
    public void onSOSSwitchInfo(BDSOSInfo.SwitchInfo switchInfo) {
        if(switchInfo!=null){
            EventBus.getDefault().post(switchInfo);
        }
    }

    @Override
    public void onOKInfo(BDOKInfo bdokInfo) {
        if(bdokInfo!=null){
            EventBus.getDefault().post(bdokInfo);
        }
    }

    @Override
    public void onOKSwitchInfo(BDOKInfo.SwitchInfo switchInfo) {

    }

    @Override
    public void onTraceInfo(BDTraceInfo bdTraceInfo) {
        if(bdTraceInfo!=null){
            EventBus.getDefault().post(bdTraceInfo);
        }
    }

    @Override
    public void onBDPRXInfo(CCPRSInfo ccprsInfo) {
        if(ccprsInfo!=null){
            EventBus.getDefault().post(ccprsInfo);
        }
    }


    @Override
    public void onScanningResult(List<BluetoothDevice> list, BluetoothDevice bluetoothDevice) {
        mBlueToothInterface.onScanningResult(list,bluetoothDevice);
    }

    @Override
    public void onConnectSucceed(BluetoothDevice bluetoothDevice) {
        BdToolFactory.getBdSendBase().initBle();
        mBlueToothInterface.onConnectSucceed(bluetoothDevice);
    }

    @Override
    public void onConnectError() {
        if(BdToolFactory.MOBILE_PHONE_TYPE==BdToolFactory.ORDINARY_MOBILE_PHONE){
            MainApp.getInstance().showMsg("蓝牙已断开!");
        }
        BeidouBoxParams.init();
        EventBus.getDefault().post("onConnectError");

    }

    @Override
    public void onDisconnect() {
        if(BdToolFactory.MOBILE_PHONE_TYPE==BdToolFactory.ORDINARY_MOBILE_PHONE){
            MainApp.getInstance().showMsg("蓝牙已断开!");
        }
        BeidouBoxParams.init();
        EventBus.getDefault().post("onConnectError");
    }

    @Override
    public void sendDataCallback(int i) {

    }

    @Override
    public void onBDMDXInfo(BDMDXInfo bdmdxInfo) {
        if(bdmdxInfo!=null){
            EventBus.getDefault().post(bdmdxInfo);
        }
    }

    @Override
    public void onBDUGXInfo(BDUGXInfo bdugxInfo) {

    }

    @Override
    public void onBDBTXInfo(BDBTXInfo bdbtxInfo) {
        if(bdbtxInfo!=null){
            EventBus.getDefault().post(bdbtxInfo);
        }
    }

    @Override
    public void onBDCEXInfo(BDCEXInfo bdcexInfo) {

    }

    @Override
    public void onBDYPXInfo(BDYPXInfo bdypxInfo) {
        if(bdypxInfo!=null){
            EventBus.getDefault().post(bdypxInfo);
        }
    }

    @Override
    public void onBDRNXInfo(BDRNXInfo bdrnxInfo) {
        if(bdrnxInfo!=null){
            EventBus.getDefault().post(bdrnxInfo);
        }
    }

    public interface BlueToothInterface {
        void onScanningResult(List<BluetoothDevice> list, BluetoothDevice bluetoothDevice);
        void onConnectSucceed(BluetoothDevice bluetoothDevice);
    }
    private BlueToothInterface mBlueToothInterface;
    public void setBlueToothInterface(BlueToothInterface blueToothInterface){
        this.mBlueToothInterface=blueToothInterface;
    }
    /**
     * 消息提示操作
     *
     * @param content
     */
    public void updateRecentChatTable(String content) {
        if(Constant.Y.equals(Constant.unread_msg_isOpen)){
            //震动和叮咚
            BeeAndVibrateManager.playBeeAndVibrate(mp -> {
            });
            //推送通知栏消息
            if (!AppUtil.isAppForeground()&&Constant.Y.equals(Constant.bg_not_isOpen)) {
                MainApp.getInstance().processCustomMessage(content);
            }
        }
    }

 /* public static void main(String[] args) {

    }*/

    public  int[] onReceiveBDPWI(String[] values){
        int index=2;
        int rdss3Count = Integer.parseInt(values[index]);
        ++index;
        int[] s21 = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        for(int i = 0; i < rdss3Count; ++i) {
            int id = Integer.parseInt(values[index]);
            if (id <= 16) {
                int number = Integer.parseInt(values[index + 1]);
                s21[id - 1] = number;
                if (values.length > index + 3) {
                    if (!"0".equals(values[index + 3]) && !"".equals(values[index + 3])) {
                        index += 3;
                    } else {
                        index += 4;
                    }
                }
            }
        }
        return s21;
    }
    public  void refreshSnrInfo(int[]s21){
        BDSnrInfo.obj.s1 = s21[0];
        BDSnrInfo.obj.s2 = s21[1];
        BDSnrInfo.obj.s3 = s21[2];
        BDSnrInfo.obj.s4 = s21[3];
        BDSnrInfo.obj.s5 = s21[4];
        BDSnrInfo.obj.s6 = s21[5];
        BDSnrInfo.obj.s7 = s21[6];
        BDSnrInfo.obj.s8 = s21[7];
        BDSnrInfo.obj.s9 = s21[8];
        BDSnrInfo.obj.s10 = s21[9];
        BDSnrInfo.obj.s11 = s21[10];
        BDSnrInfo.obj.s12 = s21[11];
        BDSnrInfo.obj.s13 = s21[12];
        BDSnrInfo.obj.s14 = s21[13];
        BDSnrInfo.obj.s15 = s21[14];
        BDSnrInfo.obj.s16 = s21[15];
        BDSnrInfo.obj.s17 = s21[16];
        BDSnrInfo.obj.s18 = s21[17];
        BDSnrInfo.obj.s19 = s21[18];
        BDSnrInfo.obj.s20 = s21[19];
        BDSnrInfo.obj.s21 = s21[20];
        bdManager.onBDSnrInfo(BDSnrInfo.obj);
    }
    public void onBdACKInfo(BdXyACKInfo ackInfo) {
        if(ackInfo != null) {
            Log.e(BdManager.class.getSimpleName(), "onBdACKInfo: " );
            if(BeidouBoxParams.lastMsg==null) {return;}
            boolean isReceived = false;
            if ("2".equals(ackInfo.type)) {
                Log.e("TAG", "接收成功" );isReceived = true;
                BeidouBoxParams.lastMsg.setStatus(Constant.SEND_MSG_RECEIVED);
            }else{
                Log.e("TAG", "入站成功");
                BeidouBoxParams.lastMsg.setStatus(Constant.SEND_MSG_INBOUND);
            }
            BeidouBoxParams.lastMsg.update();
            ResultNumberVo resultNumberVo=new ResultNumberVo();
            resultNumberVo.setNumber(BeidouBoxParams.lastMsg.getReceiverNumber());
            EventBus.getDefault().post(resultNumberVo);
            if(isReceived){BeidouBoxParams.lastMsg = null;}
        }
    }
    public static void main(String[] args) {
//        double w=Double.parseDouble(StringUtil.df2LatLng("3207.58520"));
//        double s=Double.parseDouble(StringUtil.df2LatLng("11854.12701"));
        Double a=0.04*5.144;
        System.out.println(a.floatValue());
//        System.out.println(w);
//        System.out.println(s);
    }
}
